package com.qin.controller;

import java.io.IOException;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.qin.entity.Book;
import com.qin.entity.BookCategory;
import com.qin.entity.Shopping;
import com.qin.entity.User;
import com.qin.service.BookCategoryService;
import com.qin.service.BookService;
import com.qin.service.ShoppingService;
import com.qin.service.impl.BookCategoryServiceImpl;
import com.qin.service.impl.BookServiceImpl;
import com.qin.service.impl.ShoppingServiceImpl;
import com.qin.vo.ShoppingVo;

import cn.hutool.log.StaticLog;

/**
 * 处理请求，跳转到购物车
 */
@WebServlet("/shoppingcart")
public class ShoppingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
	private ShoppingService shoppingService = new ShoppingServiceImpl();
	private BookCategoryService bookCategoryService = new BookCategoryServiceImpl();
	private BookService bookService = new BookServiceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 尝试从session中拿到用户的id，查询购物车记录
		HttpSession session = request.getSession();
		User user = (User) session.getAttribute("user");
		
		// 拿到购物车记录
		List<Shopping> shoppingItemsByUserId = shoppingService.getShoppingItemsByUserId(user.getId());
		
		// 准备一个Map，用来存放购物车记录信息
		Map<Long,ShoppingVo> shoppingCart = new HashMap<>();
		
		// 遍历购物车记录，转成VO，放入map中
		shoppingItemsByUserId.stream().forEach(shopping->{
			
			// 如果存在
			if(shoppingCart.containsKey(shopping.getBookId())){
				ShoppingVo shoppingVo = shoppingCart.get(shopping.getBookId());
				// 从新设置数量
				shoppingVo.setBookNum(shoppingVo.getBookNum()+shopping.getBookNum());
				shoppingCart.put(shopping.getBookId(), shoppingVo);
			}else {
				Book bookById = bookService.getBookById(shopping.getBookId());
				// 创造一个Vo
				ShoppingVo shoppingVo = new ShoppingVo();
				// 设置值
				shoppingVo.setId(shopping.getId());
				shoppingVo.setUserId(shopping.getUserId());
				shoppingVo.setBook(bookById);
				shoppingVo.setBookId(shopping.getBookId());
				shoppingVo.setBookNum(shopping.getBookNum());
				shoppingVo.setCreateTime(shopping.getCreateTime());
				// 放入Map
				shoppingCart.put(shoppingVo.getBookId(), shoppingVo);
			}
		});
		// 按转成List集合，按照时间排序
		Collection<ShoppingVo> values = shoppingCart.values();
		List<ShoppingVo> collect = values.stream().sorted(Comparator.comparing(ShoppingVo::getCreateTime).reversed()).collect(Collectors.toList());
		
		StaticLog.debug("购物车的信息：{}", collect);
		// 放到请求域中
		request.setAttribute("shoppings", collect);
		
		// 查出所有的一级分类
		List<BookCategory> categoryByCLevel = bookCategoryService.queryBookCategoryByCLevel(1);
		// 放在请求域中
		request.setAttribute("categoryByCLevel", categoryByCLevel);
		
		// 转发到购物车页面
		request.getRequestDispatcher("/WEB-INF/user/shopping.jsp").forward(request, response);
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
